package medium;
/*
34. 在排序数组中查找元素的第一个和最后一个位置
 */
public class LeeCode34 {

  public static void main(String[] args) {
    int[] num = new LeeCode34().searchRange(new int[]{1, 2, 3, 4, 4, 5}, 4);
    for (int i : num) {
      System.out.println(i);
    }
  }

  public int[] searchRange(int[] nums, int target) {
    int l = search(nums,target);
    int r = search(nums,target+1);
    if (l == nums.length || nums[l] !=target){
      return new int[]{-1,-1};
    }
    return new int[]{l,r - 1};
  }

  public int search(int[] nums,int target){
    int l = 0;
    int r = nums.length;
    while (l < r){
      int mid = (l + r)/2;
      if (target <= nums[mid]){
        r = mid;
      }else {
        l = mid + 1;
      }
    }
    return l;
  }
}
